package sw6.admin.gui.logic;

import java.util.ArrayList;

import sw6.admin.R;
import sw6.admin.gui.MenuEntry;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class GuiBuilder {
	public ArrayList<MenuEntry> BuildMenu(Menu menu, Context context, LayoutInflater layoutInflater) {
		ArrayList<MenuEntry> menuEntries = new ArrayList<MenuEntry>();

		for (Setting setting : menu.getSettings()) {
			MenuEntry me = new MenuEntry(setting.getRealName(), setting.getDescription());
			//if (setting.mDataType.equals("boolean"))
			menuEntries.add(me);

			if (setting.mDataType.equals("integer") || setting.mDataType.equals("double") || setting.mDataType.equals("string")) {
				final Dialog dialog = new Dialog(context);
				dialog.setContentView(R.layout.input_dialog);

				TextView text = (TextView) dialog.findViewById(R.id.text);
				text.setInputType(InputType.TYPE_CLASS_NUMBER);
				//text

				String boundDesc = "";
				final Button button = (Button) dialog.findViewById(R.id.button1);

				if (setting.mDataType.equals("integer") || setting.mDataType.equals("double")) {
					final EditText editText = (EditText) dialog.findViewById(R.id.editText);
					final Setting finalSetting = setting;
					editText.setInputType(InputType.TYPE_CLASS_NUMBER);
					editText.addTextChangedListener(new TextWatcher() {

						@Override
						public void onTextChanged(CharSequence s, int start, int before, int count) {
							if (editText.getText().toString().equals(""))
								return;
							double textValue = Double.parseDouble(editText.getText().toString());
							
							if (textValue < finalSetting.mMin  || textValue > finalSetting.mMax)
								button.setText("Ugyldig værdi");
							else
								button.setText("OK");
						}
						@Override
						public void beforeTextChanged(CharSequence s, int start, int count, int after) {
						}
						@Override
						public void afterTextChanged(Editable s) {
						}
					});
				}
				
				if (setting.mDataType.equals("string")) {
					final EditText editText = (EditText) dialog.findViewById(R.id.editText);
					final Setting finalSetting = setting;
					editText.addTextChangedListener(new TextWatcher() {
						
						@Override
						public void onTextChanged(CharSequence s, int start, int before, int count) {
							int textLength = editText.getText().toString().length();
							if (textLength == 0)
								return;
							if (textLength < finalSetting.mMin  || textLength > finalSetting.mMax)
								button.setText("Ugyldig værdi");
							else
								button.setText("OK");
						}
						@Override
						public void beforeTextChanged(CharSequence s, int start, int count, int after) {
						}
						@Override
						public void afterTextChanged(Editable s) {
						}
					});
				}

				button.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Button b = (Button)v;
						String text = b.getText().toString();
						if (text.equals("Ugyldig værdi") == false)
							dialog.dismiss();
					} 
				});

				if (setting.mDataType.equals("integer"))
				{
					if (setting.getMin() != -Double.MAX_VALUE && setting.getMax() == Double.MAX_VALUE) {
						boundDesc += "Værdien skal være over " + (int)setting.getMin();
					}
					else if (setting.getMin() == -Double.MAX_VALUE && setting.getMax() != Double.MAX_VALUE) {
						boundDesc += "Værdien må maksimalt være " + (int)setting.getMax();
					}
					else if (setting.getMin() != -Double.MAX_VALUE && setting.getMax() != Double.MAX_VALUE) {
						boundDesc += "Værdien skal være mellem " + (int)setting.getMin() + " og " + (int)setting.getMax();
					}
				}
				else if (setting.mDataType.equals("double")) {
					if (setting.getMin() != -Double.MAX_VALUE && setting.getMax() == Double.MAX_VALUE) {
						boundDesc += "Værdien skal være over " + setting.getMin();
					}
					else if (setting.getMin() == -Double.MAX_VALUE && setting.getMax() != Double.MAX_VALUE) {
						boundDesc += "Værdien må maksimalt være " + setting.getMax();
					}
					if (setting.getMin() == -Double.MAX_VALUE && setting.getMax() == Double.MAX_VALUE) {
						boundDesc += "Værdien skal være mellem " + setting.getMin() + " og " + setting.getMax();
					}
				}

				if (boundDesc == "")
					text.setText(setting.getDescription());
				else
					text.setText(setting.getDescription() + " - " + boundDesc);

				dialog.setTitle(setting.getRealName());
				setting.setDialog(dialog);
			}
			else if (setting.mDataType.equals("enum")) {

				CharSequence[] items = new CharSequence[setting.getEnumElements().size()];

				for (int i = 0; i < setting.getEnumElements().size(); i++) {
					items[i] = setting.getEnumElements().get(i).getmRealName();
				}

				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setTitle(setting.getRealName());
				builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						dialog.dismiss();
					}
				});
				AlertDialog alert = builder.create();
				
				setting.setDialog(alert);
			}
		}

		for (Menu menuItem : menu.getMenus()) {
			menuEntries.add(new MenuEntry(menuItem.getRealName(), menuItem.getDescription()));
		}

		return menuEntries;
	}
}

